package me.zhengjie.modules.system.rest.front;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.system.bo.req.YplyReq;
import me.zhengjie.modules.system.bo.resp.QztyIndexResp;
import me.zhengjie.modules.system.service.DccArticleService;
import me.zhengjie.modules.system.service.DccOthersMenuService;
import me.zhengjie.modules.system.service.DccYaopinlanyongService;
import me.zhengjie.modules.system.service.dto.DccArticleQueryCriteria;
import me.zhengjie.modules.system.service.dto.DccOthersMenuQueryCriteria;
import me.zhengjie.modules.system.service.dto.DccYaopinlanyongQueryCriteria;
import me.zhengjie.utils.RedisUtils;
import me.zhengjie.utils.StringUtils;
import org.hibernate.transform.Transformers;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author cjl
 * @className FrontIndexController
 * @description: TODO
 * @company: 上海明我信息技术有限公司
 * @date 2023年05月18日
 * @version: 1.0
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "DCC类目相关管理")
@RequestMapping("/front/dplm")
public class FrontDupinleimuController {
    private final DccYaopinlanyongService dccYaopinlanyongService;

    private final DccArticleService dccArticleService;
    private final DccOthersMenuService dccOthersMenuService;

    private final EntityManager entityManager;

    private final RedisUtils redisUtils;

    public  List<Map<String, Object>> listBySQL(String sql) {
        Query query = entityManager.createNativeQuery(sql);

        query.unwrap(org.hibernate.SQLQuery.class)
                .setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        return query.getResultList();
    }



    @GetMapping("/listV1")
    @Log("查询毒品识别分页版")
    @ApiOperation("查询毒品识别分页版")
    @AnonymousAccess
    public ResponseEntity<Object> listV1(@RequestParam(value = "keyWords",required = false) String keyWords,@RequestParam(value = "pageNo",required = true) Integer pageNo,@RequestParam(value = "pageSize",required = true) Integer pageSize){
        System.out.println("记录搜索词"+ keyWords);
        Integer start=(pageNo-1)*pageSize;

        if (StringUtils.isAllEmpty(keyWords)){
            Object queryDccOthersMenu = redisUtils.get("queryDccOthersMenulistV1"+pageNo+pageSize);
            if (ObjectUtil.isNotEmpty(queryDccOthersMenu)){
                return new ResponseEntity<>(queryDccOthersMenu,HttpStatus.OK);
            }else{
                List<Map<String, Object>> maps = listBySQL("select * from dcc_others_menu limit "+start+","+pageSize);
                redisUtils.set("queryDccOthersMenulistV1"+pageNo+pageSize,maps);
                return new ResponseEntity<>(maps,HttpStatus.OK);
            }
        }else{
            Object queryDccOthersMenu = redisUtils.get("queryDccOthersMenulistV1:"+keyWords+pageNo+pageSize);
            if (ObjectUtil.isNotEmpty(queryDccOthersMenu)){
                return new ResponseEntity<>(queryDccOthersMenu,HttpStatus.OK);
            }else{
                List<Map<String, Object>> maps = listBySQL("select * from dcc_others_menu where type like '%"+keyWords+"%' " +
                        "or  tab like '%"+keyWords+"%' or huaxue like '%"+keyWords+"%' or name like '%"+keyWords+"%' or yingwen like '%"+keyWords+"%' or cas like '%"+keyWords+"%' or lieguan like '%"+keyWords+"%' or weihua like '%"+keyWords+"%' limit "+start+","+pageSize);
                redisUtils.set("queryDccOthersMenulistV1:"+keyWords+pageNo+pageSize,maps);
                return new ResponseEntity<>(maps,HttpStatus.OK);
            }
        }
    }



    @GetMapping("/list")
    @Log("查询毒品识别")
    @ApiOperation("查询毒品识别")
    @AnonymousAccess
    public ResponseEntity<Object> queryDccOthersMenu(@RequestParam(value = "keyWords",required = false) String keyWords,@RequestParam(value = "type",required = false) String type){
        System.out.println("记录搜索词"+ keyWords);
        if (StringUtils.isAllEmpty(keyWords) && StringUtils.isAllEmpty(type)){
            Object queryDccOthersMenu = redisUtils.get("queryDccOthersMenu");
            if (ObjectUtil.isNotEmpty(queryDccOthersMenu)){
                return new ResponseEntity<>(queryDccOthersMenu,HttpStatus.OK);
            }else{
                List<Map<String, Object>> maps = listBySQL("select * from dcc_others_menu ");
                redisUtils.set("queryDccOthersMenu",maps);
                return new ResponseEntity<>(maps,HttpStatus.OK);
            }

        }else if (!StringUtils.isAllEmpty(keyWords) && StringUtils.isAllEmpty(type)){
            Object queryDccOthersMenu = redisUtils.get("queryDccOthersMenu:"+keyWords);
            if (ObjectUtil.isNotEmpty(queryDccOthersMenu)){
                return new ResponseEntity<>(queryDccOthersMenu,HttpStatus.OK);
            }else{
                List<Map<String, Object>> maps = listBySQL("select * from dcc_others_menu where type like '%"+keyWords+"%' " +
                        "or  tab like '%"+keyWords+"%' or huaxue like '%"+keyWords+"%' or name like '%"+keyWords+"%' or yingwen like '%"+keyWords+"%' or cas like '%"+keyWords+"%' or lieguan like '%"+keyWords+"%' or weihua like '%"+keyWords+"%' ");
                redisUtils.set("queryDccOthersMenu:"+keyWords,maps);
                return new ResponseEntity<>(maps,HttpStatus.OK);
            }
        } else if (StringUtils.isAllEmpty(keyWords) && !StringUtils.isAllEmpty(type)) {
            Object queryDccOthersMenu = redisUtils.get("queryDccOthersMenu:"+type);
            if (ObjectUtil.isNotEmpty(queryDccOthersMenu)){
                return new ResponseEntity<>(queryDccOthersMenu,HttpStatus.OK);
            }else{
                List<Map<String, Object>> maps = listBySQL("select * from dcc_others_menu where type like '%"+type+ "%'" + "or tab like '%"+type+"%'");
                redisUtils.set("queryDccOthersMenu",maps);
                return new ResponseEntity<>(maps,HttpStatus.OK);
            }
        }else{
            Object queryDccOthersMenu = redisUtils.get("queryDccOthersMenu:"+keyWords+type);
            List<Map<String, Object>> maps = listBySQL("select * from dcc_others_menu where (type like '%"+keyWords+ "%' " + "or  tab like '%"+keyWords+"%' or huaxue like '%"+keyWords+"%' or name like '%"+keyWords+ "%' " +
                    "or " + "yingwen like '%"+keyWords+"%' or cas like '%"+keyWords+"%' or lieguan like '%"+keyWords+"%' or " + "weihua like '%"+keyWords+"%' ) and (type like '%"+type+ "%'" + "or tab like '%"+type+"%')");
            redisUtils.set("queryDccOthersMenu:"+keyWords,maps);
            redisUtils.set("queryDccOthersMenu:"+keyWords+type,maps);
            return new ResponseEntity<>(maps,HttpStatus.OK);
        }
    }
}
